package com.lichuandi.controller;

import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class DataController {
    private List<String> sensorData = new ArrayList<>();

    // 接收设备数据
    @PostMapping("/data")
    public String receiveData(@RequestBody String data) {
        sensorData.add(data);
        System.out.println("收到数据: " + data);
        return "Data received!";
    }

    // 获取最新数据
    @GetMapping("/latest")
    public String getLatestData() {
        return sensorData.isEmpty() ? "No data" : sensorData.get(sensorData.size() - 1);
    }
}
// 访问 http://localhost:8080/api/data 发送设备数据，如：{"temperature": 25.6, "humidity": 50.3}
// 访问 http://localhost:8080/api/latest 获取最新数据
// 输出：收到数据: {"temperature": 25.6, "humidity": 50.3}
